/*
 * BlocksFile.h
 *
 *  Created on: Sep 18, 2011
 *      Author: Belen y Ezequiel
 *
 *  File of fixed blocks length.
 */

#ifndef BLOCKSFILE_H_
#define BLOCKSFILE_H_

#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include "EmptySpaceMgr.h"
#include "Block.h"

using namespace std;

class BlocksFile {

public:

	/**
	 * block 	A null reference to block. The function creates
	 * 			an object with dynamic allocation.
	 *
	 * Returns  false if the block could not be created.
	 */
	bool	createNewBlock(Block** block);

	/**
	 * Returns	length of fixed size blocks.
	 */
	unsigned long	getBlockSize();

	/**
	 * block 	A null reference to block. The function creates
	 * 			an object with dynamic allocation.
	 *
	 * Returns  false if the block is reused from the empty block list.
	 * 			true if it was created at the end of the file.
	 */
	bool	getEmptyBlock(Block** block);

	/**
	 * block 	A null reference to block. The function creates
	 * 			an object with dynamic allocation.
	 * id		the ID of the block (IDs start at 0)
	 *
	 * Returns	false if it cannot get the block (it does not exist)
	 * 			true if the block is valid (empty or not)
	 */
	bool	getBlockById(unsigned long id,Block** block);

	/**
	 * block	A not null reference to a block with a valid ID
	 * Returns	true if the block was updated, false if not (the ID
	 * 			is not valid, out of file size, or it's larger than blocks size)
	 */
	bool	updateBlock(Block *block);


	/**
	 * id		ID of block to mark as deleted
	 *
	 * Returns	true if the block was successfully marked as deleted.
	 */
	bool	deleteBlock(unsigned long id);

	/**
	 * Returns how many blocks the file has
	 */
	unsigned long getBlocksCount();

	BlocksFile(string filePath,unsigned long blockSize);
	virtual ~BlocksFile();

private:

	unsigned long	qtyBlocks;
	unsigned long	blockSize;
	unsigned long	blockCount;

	fstream _file;
	string filePath;

	// empty space manager
	EmptySpaceMgr *spaceMgr;
};


#endif /* BLOCKSFILE_H_ */
